package com.zjdiepu.www.biz.dz;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.PickCarStatus;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.OtherAttachmentsMapper;
import com.zjdiepu.www.dao.dz.DzPickCarMapper;
import com.zjdiepu.www.dao.other.OtherAuditInfoMapper;
import com.zjdiepu.www.dao.yw.YwBusinessLoanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.dz.IDzPickCarService;
import com.zjdiepu.www.ibiz.fk.IFkAuditListService;
import com.zjdiepu.www.model.dz.dto.DzPickCar;
import com.zjdiepu.www.model.dz.ext.DzPickCarExt;
import com.zjdiepu.www.model.other.dto.OtherAuditInfo;
import com.zjdiepu.www.model.other.ext.OtherAuditInfoExt;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年07月25日 14时40分<br/>
 * @since 1.0<br/>
 */
@Service
public class DzPickCarServiceImpl extends BaseService implements IDzPickCarService {
	public static final Logger logger = LoggerFactory.getLogger(DzPickCarServiceImpl.class);

	@Autowired
	private DzPickCarMapper dzPickCarMapper;
	@Autowired
	private IFkAuditListService fkAuditListService;
	@Autowired
	private YwBusinessLoanMapper ywBusinessLoanMapper;
	@Autowired
	private OtherAuditInfoMapper otherAuditInfoMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private OtherAttachmentsMapper otherAttachmentsMapper;
	
	@Override
	public ApiResponse saveConfirmation(DzPickCar dzPickCar, AbstractSysUser currentUser) {
		String busiId = dzPickCar.getBusiId();
		YwBusinessDetail ywBusinessDetail = ywBusinessMapper.selectMultiByPrimaryKey(busiId);
		if (null == ywBusinessDetail) {
			return ApiResponse.fail("999","业务数据不存在或丢失");
		}
		
    	//校验完毕，开始业务
    	String currentUserId = currentUser.getId();
    	String currentUserName = currentUser.getName();
    	String id = dzPickCar.getId();
    	dzPickCar.setPrice(null); //不可修改字段，防止错误的价格覆盖车辆价格
    	boolean commit = true;
    	if(null != dzPickCar.getSave() && dzPickCar.getSave()) {
    		commit = false;
    		dzPickCar.setPickCarStatus(PickCarStatus.YI_BAO_CUN.getCode());
    	} else {
    		if(!dzPickCar.isCompleted()) {
    			return ApiResponse.fail(901, "提车信息不完整");
    		}
    		dzPickCar.setPickCarStatus(PickCarStatus.WEI_SHEN_HE.getCode());
    	}
    	
    	if(StringUtils.isBlank(id)) {
    		dzPickCar.initInsertData(currentUserId, currentUserName);
    		dzPickCarMapper.insert(dzPickCar);
    	} else {
    		dzPickCar.initUpdateData(currentUser.getId(), currentUser.getName());
    		// 影响的记录数
    		int updateCount = dzPickCarMapper.updateByPrimaryKeySelective(dzPickCar);
    		if(updateCount == 0){
    			return ApiResponse.fail(ResultFlagType.MISSING);
    		} else if(updateCount > 1){
    			return ApiResponse.fail(ResultFlagType.MULTI_DATA);
    		}
    	}
    	
    	if(commit) {
//    		@update 2017-07-25 xiongxiaotun	新增提车待审核
    		//资审一审核人
    		OtherAuditInfoExt otherAuditInfoExt = new OtherAuditInfoExt();
    		otherAuditInfoExt.setBusiId(busiId);
    		otherAuditInfoExt.setBaseWhere();
    		otherAuditInfoExt.setRows(1);
    		otherAuditInfoExt.setModeId(ModeIDType.ZSYS.getCode());
    		otherAuditInfoExt.setOrderSql("`CREATED` DESC");
    		List<OtherAuditInfo> otherAuditInfos = otherAuditInfoMapper.selectByExt(otherAuditInfoExt);
    		String ziShenYiShenName = null;
    		if(CollectionUtils.isNotEmpty(otherAuditInfos)) {
    			OtherAuditInfo otherAuditInfo = otherAuditInfos.get(0);
    			ziShenYiShenName = otherAuditInfo.getCreatorName();
    		}
    		fkAuditListService.insertFkAuditList(ywBusinessDetail, ModeIDType.XCTCSH, "新车提车", currentUser, ziShenYiShenName);
    	}
		return ApiResponse.success(dzPickCar.getId());
	}
	
	@Override
	public void saveUpdateAndDelete(DzPickCar dzPickCar, OperateType operType) {
		switch (operType) {
		case add:
			dzPickCar.initInsertData(dzPickCar.getModifier(), dzPickCar.getModifierName());
			dzPickCarMapper.insert(dzPickCar);
			break;
		case edit:
			dzPickCar.initUpdateData(dzPickCar.getModifier(), dzPickCar.getModifierName());
			// 影响的记录数
			int updateCount = dzPickCarMapper.updateByPrimaryKeySelective(dzPickCar);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dzPickCar.initDeleteData(dzPickCar.getModifier(), dzPickCar.getModifierName());
			// 影响的记录数
			int deleteCount = dzPickCarMapper.updateByPrimaryKeySelective(dzPickCar);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DzPickCar> selectByExt(DzPickCarExt dzPickCarExt) {
		return dzPickCarMapper.selectByExt(dzPickCarExt);
	}
	
	@Override
	public int countByExt(DzPickCarExt dzPickCarExt){
		return dzPickCarMapper.countByExt(dzPickCarExt);
	}
	
	@Override
	public DzPickCar selectById(String id) {
		return dzPickCarMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dzPickCarMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DzPickCar dzPickCar) {
		return dzPickCarMapper.countRefined(dzPickCar);
	}

	@Override
	public DzPickCar selectRefined(DzPickCar dzPickCar) {
		return dzPickCarMapper.selectRefined(dzPickCar);
	}
	
	@Override
	public int deletedRefined(DzPickCar dzPickCar) {
		return dzPickCarMapper.deletedRefined(dzPickCar);
	}

	@Override
	public boolean repeat(DzPickCar dzPickCar) {
		int count = countRefined(dzPickCar);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dzPickCar.getId())) {
			return true;
		}
		DzPickCar dbItem = selectRefined(dzPickCar);
		if(null == dbItem) {
			return false;
		}
		return !dzPickCar.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dzPickCarMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DzPickCar> dzPickCars) {
		return dzPickCarMapper.insertBatch(dzPickCars);
	}
}
